<?php
class Crud extends Model {

	var	$data =	array();
	var	$form =	array();
	var	$controller;
	
	function Crud() {
		parent::Model();
		$this->load->helper('form');
		$this->load->helper('url');
		//$this->load->library('errors');
		$this->load->library('validation');
		$this->load->database();
		$this->load->model('display');
	}

	function showall($controller='', $message =	'',	$test ='no') {
		$result	= '';
		$mysess	=	$this->session->userdata('session_id');
		$mystat	=	$this->session->userdata('status');
		if(!$this->db->table_exists($controller)) {
			$place =	__FILE__.__LINE__;
			$outcome = "exception:$place:looking for table $controller:	it doesn't exist'";
			if($test =='yes') {
				return $outcome;
			}
			else {
				$this->failure($outcome	, 'sites');
			}
		}

		$this->db->select('id, catagory_name');
		$query = $this->db->get($controller);
		if ($query->num_rows() > 0)	{
			$result	.= "<table class='table'>";
			$result	.= "<tr><td	colspan='3'><h3>$controller</h3></td></tr>";  
			$result	.= "<tr><td	colspan='3'	class='message'>$message</td></tr>"; 
			$result	.= "<tr><td	colspan='3'>";
			$result	.= anchor("$controller/insert/0", 'New entry');
			$result	.=	"</td></tr>";
			$result	  .= "<tr><td colspan='3'>";
			$result	.=	anchor("$controller/read", 'Show all entries in	the	table');	
			$result	.=	"</td></tr>";
			foreach	($query->result() as $row) {
				$result	.= "<tr><td>";
				$result	.= $row->id;
				$result	.= "	";
				$result	.= $row->catagory_name;
				$result	.= "</td><td>";
				$result	.= anchor("$controller/insert/$row->id",'Update	this	entry');
				$result	.= "</td><td>";
				$result	.= anchor("$controller/delete/$row->id",'Delete');
				$result	.= "</td></tr>";
			}
			$result	.= "</table>";
			$data['text'] =	$result;
			$this->display->mainpage($data,	$this->status);
		}
		else {
			$place	=  __FILE__.__LINE__;
			$outcome = "exception: $place: no results from table $controller";
			 if($test == 'yes')	{
				$place =  __FILE__.__LINE__;
				return $outcome;
			}
			else {
				$message = "No data	in the $controller table";
				$this->failure($message,	'sites');
			}
		}	
	}

}
?>